/*
 * 文件名:CAlsa.hpp
 *
 * 	 功能描述:
 *  创建日期: 2010-11-27
 *  版本Ver0.01
 *  作者: huanying2000
 *  版权:copyright 2010 by the Camellia project 
 *  free software under the terms of the LGPL 2.1
 */

#ifndef CALSA_HPP_
#define CALSA_HPP_

#include <alsa/asoundlib.h>
#include <alsa/mixer.h>
#include <linux/soundcard.h>
#include "Globel.hpp"
#include "COutput.h"

/* make ALSA 0.9.x compatible to the 1.0.x API */
#define ALSA_PCM_NEW_HW_PARAMS_API
#define ALSA_PCM_NEW_SW_PARAMS_API
#define BUFFER_LENGTH 0.5						/* in seconds */

class CAlsa : public COutput
{
	public:
		CAlsa();
		~CAlsa();
		int openOutput(KAudioOutput *ao);
		int writeOutput(KAudioOutput *ao, unsigned char *buf, int bytes);
		int closeOutput(KAudioOutput *ao);
		void flushOutput(KAudioOutput *ao);
		int setVolume(long val);

	private:
		//alsa格式与mpg123格式对应关系
		struct KFormatMap
		{
			snd_pcm_format_t alsaFormat;
			int mpg123Format;
		};

		static const int formatNumber = 10;
		KFormatMap formatMap[formatNumber];

		snd_mixer_t *mixerFd;
		snd_mixer_elem_t *mixerElem;

		int mixer;

		int ratesMatch(long int desired, unsigned int actual);
		int initializeDevice(KAudioOutput *ao);
		int getFormatsAlsa(KAudioOutput *ao);
		int initializeMixter();
};

#endif /* CALSA_HPP_ */
